package com.hnevc.mytrafficdemo.util.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.hnevc.mytrafficdemo.entity.RechargeRecord;

/**
 * Created by Administrator on 2017/12/20.
 */

public class RechargeRecordDBUtil {
    Context mContext;

    RecorderSQLiteOpenHelper mOpenHelper;

    public RechargeRecordDBUtil(Context context) {
        mOpenHelper = new RecorderSQLiteOpenHelper(context,"rechargerecord.db",null,1);


    }

    /**
     * 添加记录
     * @param record
     * @return
     */
    public boolean insert(RechargeRecord record){

        SQLiteDatabase writeDB = mOpenHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("carNum",record.getCarNum());
        values.put("rechargeAmount",record.getRechargeAmount());
        values.put("balance",record.getBalance());
        values.put("recharger",record.getRecharger());
        values.put("rechargeDate",record.getRechargeDate());
        long result = writeDB.insert("rechargerecord",null,values);
        writeDB.close();
        return result!=-1;
    }

    public Cursor query(){
        SQLiteDatabase readDB = mOpenHelper.getReadableDatabase();
        return readDB.query("rechargerecord",null,null,null,null,null,null);
    }


    class RecorderSQLiteOpenHelper  extends SQLiteOpenHelper{

        public RecorderSQLiteOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
            super(context, name, factory, version);
        }

        @Override
        public void onCreate(SQLiteDatabase sqLiteDatabase) {
            String sql="create table rechargerecord(id integer primary key autoincrement ,carNum varchar(20)" +
                    ", rechargeAmount integer, balance integer," +
                    " recharger var(20),rechargeDate var(20))";
            sqLiteDatabase.execSQL(sql);
        }

        @Override
        public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

        }
    }
}
